<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>linux自动挂载 | chilono!</title><script src="https://cdn.bootcss.com/valine/1.4.4/Valine.min.js"></script><link rel="stylesheet" href="/css/arknights.css"><link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/highlight.js/10.1.2/styles/atom-one-dark-reasonable.min.css"><style>@font-face {
 font-family: BenderLight;
 src: local('Bender'), url("/font/BenderLight.ttf");
}
@font-face {
 font-family: 'JetBrains Mono';
 src: local('JetBrains Mono'), url('/font/JetBrainsMono-Regular.woff2') format('woff2');
}</style><meta name="generator" content="Hexo 5.0.2"></head><body><header><nav><a href="/">Home</a><a href="/archives/">Archives</a><a href="/about/">About</a></nav></header><main><article><div id="post-bg"><div id="post-title"><div id="post-info"><span>date:<time datetime="2019-03-11T15:33:16.000Z" id="date"> 2019-03-11</time></span><br><span>updated:<time datetime="2020-08-22T13:29:14.631Z" id="updated"> 2020-08-22</time></span></div><h1>linux自动挂载</h1><hr></div><div id="post-content"><p>linux的磁盘，使用mount命令挂载只是临时的，长久使用需要将写入fstab文件中才行</p>
<p>fstab文件路径：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">/etc/fstab<br></code></pre></td></tr></table></figure>

<h1 id="fstab内容分析"><a href="#fstab内容分析" class="headerlink" title="fstab内容分析"></a>fstab内容分析</h1><p>fstab文件内写的都是开机自动挂载的一些所需信息，我们先看一下实际文件进行参考：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta">#</span><span class="bash"> &lt;file system&gt;                           &lt;dir&gt;   &lt;<span class="hljs-built_in">type</span>&gt;  &lt;options&gt;          &lt;dump&gt;  &lt;pass&gt;</span><br>UUID=180e5c80-03ec-404d-a097-8c4dafcc7245 /       ext4    errors=remount-ro  0       1        // 根目录挂载<br>UUID=c5942ed0-1fd3-4093-8edc-bd918ad0cca8 none    swap    sw                 0       0        // 交换分区<br></code></pre></td></tr></table></figure>

<ol>
<li>第一列为设备号或卷标，使用UUID需要加上<code>UUID=</code></li>
<li>第二列为挂载点，就是挂载的路径</li>
<li>第三列填文件系统</li>
<li>第四列文件系统参数，没有特殊要求默认<code>defaults</code></li>
<li>第五列dump备份，默认0</li>
<li>开机检验，根目录1，其他数据盘默认2</li>
</ol>
<h1 id="实际操作"><a href="#实际操作" class="headerlink" title="实际操作"></a>实际操作</h1><p>现在我们先假设插上了一块新硬盘，具体实际操作见具体情况，虚拟机添加一块新的虚拟硬盘，物理机插上一块新物理硬盘。</p>
<h2 id="查看硬盘"><a href="#查看硬盘" class="headerlink" title="查看硬盘"></a>查看硬盘</h2><p>使用<code>fdisk</code>查看硬盘</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta">#</span><span class="bash"> fdisk -l  // 注：需要root权限</span><br><br>Disk /dev/sdb: 2 GiB, 2147483648 bytes, 4194304 sectors<br>Units: sectors of 1 * 512 = 512 bytes<br>Sector size (logical/physical): 512 bytes / 512 bytes<br>I/O size (minimum/optimal): 512 bytes / 512 bytes<br></code></pre></td></tr></table></figure>

<p>我在虚拟机中添加了一块2G的虚拟硬盘，在<code>fdisk</code>中显示出他没有进行任何分区，首先我们将它分区</p>
<h2 id="硬盘分区"><a href="#硬盘分区" class="headerlink" title="硬盘分区"></a>硬盘分区</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta">#</span><span class="bash"> fdisk /dev/sdb    // 设备路径请根据实际情况更改</span><br><br>Command (m for help): n     // 输入n创建新分区<br>Select (default p):         // 什么都不输入，默认创建主分区<br>Partition number (1-4, default 1):              // 卷标号默认不改动<br>// 本次操作只是演示，下列创建卷大小请自行更改<br>First sector (2048-4194303, default 2048):<br>Last sector, +sectors or +size&#123;K,M,G,T,P&#125; (2048-4194303, default 4194303):<br><br>Created a new partition 1 of type &#x27;Linux&#x27; and of size 2 GiB.    // 创建成功<br><br>Command (m for help): w     // 输入w保存刚才的操作<br></code></pre></td></tr></table></figure>

<p>分区创建完成后，我们还需要对分区进行格式化，这里我是当做数据盘做的，所以我将它格式化为<code>ext4</code>，若是要作他用，如交换分区，请自行查找正确操作方法。</p>
<h2 id="格式化"><a href="#格式化" class="headerlink" title="格式化"></a>格式化</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta">#</span><span class="bash"> mkfs.ext4 /dev/sdb1     // 注意这里格式的是sdb1而不是sdb</span><br></code></pre></td></tr></table></figure>

<h2 id="获取UUID"><a href="#获取UUID" class="headerlink" title="获取UUID"></a>获取UUID</h2><p>要获取UUID，我可以使用命令<code>lsblk -f</code>来得到，该命令不加参数会得到所有设备的UUID，若有参数则只显示参数设备的UUID。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta">#</span><span class="bash"> lsblk -f /dev/sdb   // 获取UUID</span><br>NAME   FSTYPE LABEL UUID                                 MOUNTPOINT<br>sdb<br>└─sdb1 ext4         f68f26c4-ba4f-4ff1-aedb-b1b88e8327e9<br></code></pre></td></tr></table></figure>

<h2 id="写入fstab文件"><a href="#写入fstab文件" class="headerlink" title="写入fstab文件"></a>写入fstab文件</h2><p>打开fstab文件将获取的UUID按格式填入，并填入挂载点，此为演示我就挂载在根目录下新建的文件夹，实际情况请根据需求更改：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta">#</span><span class="bash"> &lt;file system&gt;                           &lt;dir&gt;   &lt;<span class="hljs-built_in">type</span>&gt;  &lt;options&gt;          &lt;dump&gt;  &lt;pass&gt;</span><br>UUID=180e5c80-03ec-404d-a097-8c4dafcc7245 /       ext4    errors=remount-ro  0       1        // 根目录挂载<br>UUID=c5942ed0-1fd3-4093-8edc-bd918ad0cca8 none    swap    sw                 0       0        // 交换分区<br>UUID=f68f26c4-ba4f-4ff1-aedb-b1b88e8327e9 /new    ext4    default            0       2        // 新添加的硬盘<br></code></pre></td></tr></table></figure>

<p>以上，重启就会自动挂载啦。</p>
<h2 id="查看并验证"><a href="#查看并验证" class="headerlink" title="查看并验证"></a>查看并验证</h2><p>这里的话，个人目前才学疏浅，此为个人笔记类型，我使用<code>df -h</code>命令来查看</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta">#</span><span class="bash"> df -h</span><br>Filesystem      Size  Used Avail Use% Mounted on<br>udev            992M     0  992M   0% /dev<br>tmpfs           201M  2.9M  198M   2% /run<br>/dev/sda1        14G  4.7G  8.4G  37% /<br>tmpfs          1003M     0 1003M   0% /dev/shm<br>tmpfs           5.0M     0  5.0M   0% /run/lock<br>tmpfs          1003M     0 1003M   0% /sys/fs/cgroup<br>/dev/sdb1       2.0G  6.0M  1.9G   1% /new              // 此处是新挂载的硬盘<br>tmpfs           201M     0  201M   0% /run/user/1000<br></code></pre></td></tr></table></figure>

<p>上面倒数第二行就是我们刚才挂载的新硬盘啦，好，完成。</p>
<div id="paginator"></div></div><div id="post-footer"><hr><a href="/2019/06/24/vs%E4%B8%AD%E4%BD%BF%E7%94%A8sdl%E9%9C%80%E8%A6%81%E5%AE%9A%E4%B9%89%E5%85%A5%E5%8F%A3%E7%82%B9%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95/">← Prev VS中使用SDL需要定义入口点解决办法</a><span style="color: #fe2"> | </span><a href="/2019/03/09/%E4%BD%BF%E7%94%A8xampp%E5%92%8Cwordpress%E6%90%AD%E5%BB%BA%E4%B8%AA%E4%BA%BA%E5%8D%9A%E5%AE%A2/">使用xampp和wordpress搭建个人博客 Next →</a><hr></div><div id="bottom-btn"><a id="to-index" href="#post-index" title="index">≡</a><a id="to-top" href="#post-title" title="to top">∧</a></div><div id="Valine"></div><script>new Valine({
 el: '#Valine'
 , appId: 'w0GpBUKuLLybCyT9IuCKjvz5-9Nh9j0Va'
 , appKey: 'RHBH0vdwxr2ikIt32lBoqK69'
 , placeholder: '此条评论委托企鹅物流发送'
})</script></div></article><aside><div id="about"><a href="/" id="logo"><img src="https://ak.hypergryph.com/assets/index/images/ak/pc/faction/1.png" alt="Logo"></a><h1 id="Dr"><a href="/"> Dr.Chilono</a></h1><section id="total"><a id="total-archives" href="/archives"><span class="total-title">Archives Total:</span><span class="total-number">14</span></a><div id="total-tags"><span class="total-title">Tags:</span><span class="total-number">35</span></div><div id="total-categories"><span class="total-title">Categories:</span><span class="total-number">3</span></div></section></div><div id="aside-block"><h1>INDEX</h1><div id="post-index"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#fstab%E5%86%85%E5%AE%B9%E5%88%86%E6%9E%90"><span class="toc-number">1.</span> <span class="toc-text">fstab内容分析</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%AE%9E%E9%99%85%E6%93%8D%E4%BD%9C"><span class="toc-number">2.</span> <span class="toc-text">实际操作</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E7%A1%AC%E7%9B%98"><span class="toc-number">2.1.</span> <span class="toc-text">查看硬盘</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%A1%AC%E7%9B%98%E5%88%86%E5%8C%BA"><span class="toc-number">2.2.</span> <span class="toc-text">硬盘分区</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A0%BC%E5%BC%8F%E5%8C%96"><span class="toc-number">2.3.</span> <span class="toc-text">格式化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96UUID"><span class="toc-number">2.4.</span> <span class="toc-text">获取UUID</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%86%99%E5%85%A5fstab%E6%96%87%E4%BB%B6"><span class="toc-number">2.5.</span> <span class="toc-text">写入fstab文件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E5%B9%B6%E9%AA%8C%E8%AF%81"><span class="toc-number">2.6.</span> <span class="toc-text">查看并验证</span></a></li></ol></li></ol></div></div><footer><nobr><span class="text-title">©</span><span class="text-content">1970 to 2020</span></nobr><wbr><nobr><span class="text-title">ICP</span><span class="text-content">——备案号——</span></nobr><wbr><wbr><nobr>published with&nbsp;<a target="_blank" rel="noopener" href="http://hexo.io">Hexo&nbsp;</a></nobr><wbr><nobr>Theme&nbsp;<a target="_blank" rel="noopener" href="https://github.com/Yue-plus/hexo-theme-arknights">Arknight&nbsp;</a></nobr><wbr><nobr>by&nbsp;<a target="_blank" rel="noopener" href="https://github.com/Yue-plus">Yue_plus</a></nobr></footer></aside></main><script src="/js/arknights.js"></script><script src="https://cdn.bootcdn.net/ajax/libs/highlight.js/10.1.2/highlight.min.js"></script></body></html>